library(tidyverse)
library(fixest)
library(showtext)
library(vtable)
library(writexl)
library(haven)
library(gridExtra)

setwd('D:\\WorkingPaper-Series\\Township_Light\\Replication_Files\\workdata')

#################################
########## Figure 9 #############
#################################

firm_exit <- read_delim('twfe_exit_firm.csv')

firm_exit <- firm_exit %>% mutate(upper_ci = coef + 1.96*std_err, lower_ci = coef - 1.96*std_err)

firm_exit %>% ggplot(aes(period,coef,color = sample,group = sample))+
  geom_hline(yintercept = 0,color = 'black', lwd = 0.5, lty = 2)+
  geom_vline(xintercept = -1, color = 'grey50', lwd = 0.5, lty = 4)+
  geom_rect(aes(xmin = -4.5, xmax = -1.5, ymin = -Inf, ymax = Inf),
            fill = "grey80", alpha = 0.01,show.legend = F,inherit.aes = F)+
  geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci), width=.2, lwd = .3,position=position_dodge(0.3))+
  geom_point(aes(shape = sample, color = sample, fill = sample),position=position_dodge(0.3)) +
  theme_bw()+
  theme(legend.position = c(0.15,0.85),
        legend.background = element_rect(color = 'black', size = 0.1,linewidth = 0.1, fill = 'white'),
        legend.margin = margin(t = -0.45, r = 0.1, b = 0, l = 0.1, unit = "cm"),
        text = element_text(family = 'serif',size = 10),
        axis.text =  element_text(family = 'serif',size = 8))+
  labs(y = '', x = 'Relative Time') +
  scale_x_continuous(breaks = c(seq(-6,4,1)), labels = c('-6+','-5','-4','-3','-2','-1','0','1','2','3','4'))+
  scale_y_continuous(limits = c(-0.1,0.2))+
  scale_color_manual(values = c('full' = 'black', 'after16' = '#5C5C5C'),
                     labels = c('full' = 'Full Sample', 'after16' = 'Cohort >= 2016')) +
  scale_fill_manual(values = c('full' = 'black', 'after16' = '#5C5C5C'),
                    labels = c('full' = 'Full Sample', 'after16' = 'Cohort >= 2016')) +
  scale_shape_manual(values = c('full' = 21, 'after16' = 23),
                     labels = c('full' = 'Full Sample', 'after16' = 'Cohort >= 2016')) +
  guides(colour = guide_legend(""),fill = guide_legend(""),shape = guide_legend(""))
